# -*- coding: utf-8 -*-
from applications.extensions.db_plugin import db
from common.utils.helpers import now_beijing

"""
使用 sqlite 作为数据库时，外键约束要加上 name，否则迁移时会有问题，例如：
db.ForeignKey('user.id', name='fk_expenses_icon_user')
"""


class Icon(db.Model):
    """用于储存 icon 的表"""
    __tablename__ = 'expenses_icon'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('user.id', name='fk_expenses_icon_user'),
        index=True,
        comment="归属用户ID"
    )
    name = db.Column(db.String(50), comment="标签名称")
    icon_class = db.Column(db.String(50), comment="图标样式类名")
    is_delete = db.Column(db.Boolean, default=False, comment="是否删除")
    is_active = db.Column(db.Boolean, default=True, comment="是否启用")
    create_time = db.Column(
        db.DateTime,
        default=now_beijing,
        nullable=False,
        comment="创建时间"
    )
    update_time = db.Column(
        db.DateTime,
        default=now_beijing,
        onupdate=now_beijing,
        nullable=False,
        comment="更新时间"
    )

    user = db.relationship("User", backref=db.backref("tag", lazy="dynamic"))


class Category(db.Model):
    """用于储存账单分类的表"""
    __tablename__ = 'expenses_category'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('user.id', name='fk_expenses_category_user'),
        index=True,
        comment="归属用户ID"
    )
    name = db.Column(db.String(80), comment="分类名称")
    type = db.Column(db.Integer, comment="收入或支出，0=收入，1=支出")
    icon_class = db.Column(db.String(80), comment="图标样式类名")
    icon_color = db.Column(db.String(80), comment="图标背景颜色")
    is_delete = db.Column(db.Boolean, default=False, comment="是否删除")
    is_active = db.Column(db.Boolean, default=True, comment="是否启用")
    create_time = db.Column(
        db.DateTime,
        default=now_beijing,
        nullable=False,
        comment="创建时间"
    )
    update_time = db.Column(
        db.DateTime,
        default=now_beijing,
        onupdate=now_beijing,
        nullable=False,
        comment="更新时间"
    )

    user = db.relationship("User", backref=db.backref("category", lazy="dynamic"))


class Transaction(db.Model):
    """用于储存账单的表"""
    __tablename__ = 'expenses_transaction'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('user.id', name='fk_expenses_transaction_user'),
        index=True,
        comment="归属用户ID"
    )
    category_id = db.Column(
        db.Integer,
        db.ForeignKey('expenses_category.id', name='fk_expenses_transaction_category'),
        index=True,
        comment="归属分类ID"
    )
    type = db.Column(db.Integer, comment="收入或支出，0=收入，1=支出")
    amount = db.Column(db.Float, comment="账单金额")
    description = db.Column(db.String(120), comment="账单描述")
    date = db.Column(db.Date, comment="账单日期：年月日")
    is_delete = db.Column(db.Boolean, default=False, comment="是否删除")
    create_time = db.Column(
        db.DateTime,
        default=now_beijing,
        nullable=False,
        comment="创建时间"
    )
    update_time = db.Column(
        db.DateTime,
        default=now_beijing,
        onupdate=now_beijing,
        nullable=False,
        comment="更新时间"
    )

    user = db.relationship("User", backref=db.backref("transaction", lazy="dynamic"))
    category = db.relationship("Category", backref=db.backref("transaction", lazy="dynamic"))
